/*     */ package com.zimbra.cs.mime;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.convert.ConversionException;
/*     */ import com.zimbra.cs.index.Fragment;
/*     */ import com.zimbra.cs.index.Fragment.Source;
/*     */ import com.zimbra.cs.index.IndexDocument;
/*     */ import com.zimbra.cs.index.ZimbraAnalyzer;
/*     */ import com.zimbra.cs.index.analysis.RFC822AddressTokenStream;
/*     */ import com.zimbra.cs.store.Blob;
/*     */ import com.zimbra.cs.store.StoreManager;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ParsedDocument
/*     */ {
/*     */   private final Blob blob;
/*     */   private final long size;
/*     */   private final String digest;
/*     */   private final String contentType;
/*     */   private final String filename;
/*     */   private final String creator;
/*     */   private IndexDocument document;
/*     */   private String fragment;
/*     */   private final long createdDate;
/*     */   private final String description;
/*     */   private final boolean descEnabled;
/*  52 */   private int version = 0;
/*     */   
/*     */ 
/*     */ 
/*  56 */   private boolean temporaryAnalysisFailure = false;
/*  57 */   private boolean parsed = false;
/*     */   
/*     */   private static Blob saveInputAsBlob(InputStream in) throws ServiceException, IOException {
/*  60 */     return StoreManager.getInstance().storeIncoming(in);
/*     */   }
/*     */   
/*     */   public ParsedDocument(InputStream in, String filename, String ctype, long createdDate, String creator, String description) throws ServiceException, IOException
/*     */   {
/*  65 */     this(saveInputAsBlob(in), filename, ctype, createdDate, creator, description, true);
/*     */   }
/*     */   
/*     */   public ParsedDocument(InputStream in, String filename, String ctype, long createdDate, String creator, String description, boolean descEnabled) throws ServiceException, IOException
/*     */   {
/*  70 */     this(saveInputAsBlob(in), filename, ctype, createdDate, creator, description, descEnabled);
/*     */   }
/*     */   
/*     */   public ParsedDocument(Blob blob, String filename, String ctype, long createdDate, String creator, String description, boolean descEnabled) throws IOException
/*     */   {
/*  75 */     this.blob = blob;
/*  76 */     this.size = blob.getRawSize();
/*  77 */     this.digest = blob.getDigest();
/*  78 */     this.contentType = ctype;
/*  79 */     this.filename = StringUtil.sanitizeFilename(filename);
/*  80 */     this.createdDate = createdDate;
/*  81 */     this.creator = creator;
/*  82 */     this.description = description;
/*  83 */     this.descEnabled = descEnabled;
/*  84 */     if (!LC.documents_disable_instant_parsing.booleanValue()) {
/*  85 */       performExtraction();
/*     */     }
/*     */   }
/*     */   
/*     */   private synchronized void performExtraction()
/*     */   {
/*     */     try
/*     */     {
/*  93 */       long start = System.currentTimeMillis();
/*  94 */       MimeHandler handler = MimeHandlerManager.getMimeHandler(this.contentType, this.filename);
/*  95 */       assert (handler != null);
/*     */       
/*  97 */       if (handler.isIndexingEnabled()) {
/*  98 */         handler.init(new BlobDataSource(this.blob, this.contentType));
/*     */       }
/* 100 */       handler.setFilename(this.filename);
/* 101 */       handler.setPartName("top");
/* 102 */       handler.setSize(this.size);
/*     */       
/* 104 */       String textContent = "";
/*     */       try {
/* 106 */         textContent = handler.getContent();
/*     */       } catch (MimeHandlerException e) {
/* 108 */         if (ConversionException.isTemporaryCauseOf(e)) {
/* 109 */           ZimbraLog.doc.warn("Temporary failure extracting from the document.  (is convertd down?)", e);
/* 110 */           this.temporaryAnalysisFailure = true;
/*     */         } else {
/* 112 */           ZimbraLog.index.warn("Failure indexing wiki document " + this.filename + ".  Item will be partially indexed", e);
/*     */         }
/*     */       }
/* 115 */       this.fragment = Fragment.getFragment(textContent, Fragment.Source.NOTEBOOK);
/*     */       
/* 117 */       this.document = new IndexDocument(handler.getDocument());
/* 118 */       this.document.addSubject(this.filename);
/*     */       
/* 120 */       if (this.version > 0) {
/* 121 */         this.document.addVersion(this.version);
/*     */       }
/*     */       
/* 124 */       StringBuilder content = new StringBuilder();
/* 125 */       appendToContent(content, this.filename);
/* 126 */       appendToContent(content, ZimbraAnalyzer.getAllTokensConcatenated("filename", this.filename));
/* 127 */       appendToContent(content, textContent);
/* 128 */       appendToContent(content, this.description);
/*     */       
/* 130 */       this.document.addContent(content.toString());
/* 131 */       this.document.addFrom(new RFC822AddressTokenStream(this.creator));
/* 132 */       this.document.addFilename(this.filename);
/* 133 */       long elapsed = System.currentTimeMillis() - start;
/* 134 */       ZimbraLog.doc.debug("ParsedDocument performExtraction elapsed=" + elapsed);
/*     */     } catch (MimeHandlerException mhe) {
/* 136 */       if (ConversionException.isTemporaryCauseOf(mhe)) {
/* 137 */         ZimbraLog.doc.warn("Temporary failure extracting from the document.  (is convertd down?)", mhe);
/* 138 */         this.temporaryAnalysisFailure = true;
/*     */       } else {
/* 140 */         ZimbraLog.doc.error("cannot create ParsedDocument", mhe);
/*     */       }
/*     */     } catch (Exception e) {
/* 143 */       ZimbraLog.index.warn("Failure indexing wiki document " + this.filename + ".  Item will be partially indexed", e);
/*     */     } finally {
/* 145 */       this.parsed = true;
/*     */     }
/*     */   }
/*     */   
/*     */   private static final void appendToContent(StringBuilder sb, String s) {
/* 150 */     if (s == null) {
/* 151 */       return;
/*     */     }
/* 153 */     if (sb.length() > 0) {
/* 154 */       sb.append(' ');
/*     */     }
/* 156 */     sb.append(s);
/*     */   }
/*     */   
/*     */   public void setVersion(int version) {
/* 160 */     this.version = version;
/*     */     
/* 162 */     if (this.document == null) {
/* 163 */       ZimbraLog.doc.warn("Can't index document version.  (is convertd down?)");
/*     */     } else {
/* 165 */       this.document.addVersion(version);
/*     */     }
/*     */   }
/*     */   
/*     */   public long getSize() {
/* 170 */     return this.size;
/*     */   }
/*     */   
/*     */   public String getDigest() {
/* 174 */     return this.digest;
/*     */   }
/*     */   
/*     */   public Blob getBlob() {
/* 178 */     return this.blob;
/*     */   }
/*     */   
/*     */   public String getFilename() {
/* 182 */     return this.filename;
/*     */   }
/*     */   
/*     */   public String getContentType() {
/* 186 */     return this.contentType;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public IndexDocument getDocument()
/*     */   {
/* 193 */     if (!this.parsed) {
/* 194 */       performExtraction();
/*     */     }
/*     */     
/* 197 */     return this.document;
/*     */   }
/*     */   
/*     */   public List<IndexDocument> getDocumentList() {
/* 201 */     if (!this.parsed) {
/* 202 */       performExtraction();
/*     */     }
/* 204 */     return this.document == null ? Collections.emptyList() : Collections.singletonList(this.document);
/*     */   }
/*     */   
/*     */   public String getFragment() {
/* 208 */     if (!this.parsed) {
/* 209 */       performExtraction();
/*     */     }
/* 211 */     return this.fragment;
/*     */   }
/*     */   
/*     */   public String getCreator() {
/* 215 */     return this.creator;
/*     */   }
/*     */   
/*     */   public long getCreatedDate() {
/* 219 */     return this.createdDate;
/*     */   }
/*     */   
/*     */   public String getDescription() {
/* 223 */     return this.description;
/*     */   }
/*     */   
/*     */   public boolean isDescriptionEnabled() {
/* 227 */     return this.descEnabled;
/*     */   }
/*     */   
/*     */   public boolean hasTemporaryAnalysisFailure() {
/* 231 */     return this.temporaryAnalysisFailure;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mime/ParsedDocument.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */